package com.doeis.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;
import java.util.List;

import com.doeis.minaserver.dao.User;
import com.doeis.minaserver.dao.UserDao;
import com.doeis.minaserver.packet.NettyMessageDecoder;
import com.doeis.minaserver.packet.NettyMessageEncoder;
import com.google.inject.Guice;
import com.google.inject.Injector;
  
/**  
 * mina客户端  
 * @author aniyo   
 * blog:http://aniyo.iteye.com
 */  
public class MinaClient {   
  
//    private ScheduledExecutorService executor = Executors
//    	    .newScheduledThreadPool(1);
        EventLoopGroup group = new NioEventLoopGroup();

        public void connect(final int port, final String host, final String login,final String pwd) throws Exception {

    	// 配置客户端NIO线程组

    	try {
    	    Bootstrap b = new Bootstrap();
    	    b.group(group).channel(NioSocketChannel.class)
    		    .option(ChannelOption.TCP_NODELAY, true)
    		    .handler(new ChannelInitializer<SocketChannel>() {
						@Override
						protected void initChannel(SocketChannel socketChannel) throws Exception {
							socketChannel.pipeline().addLast(new NettyMessageDecoder());
							socketChannel.pipeline().addLast(new NettyMessageEncoder());
							socketChannel.pipeline().addLast(new NettyClientChannelHandlerAdapter(login, pwd));
						}
							
					});
    	    // 发起异步连接操作
    	    ChannelFuture future = b.connect( new InetSocketAddress(host, port)).sync();
    	    future.channel().closeFuture();
    	} finally {
    	    // 所有资源释放完成之后，清空资源，再次发起重连操作
//    		System.err.println("所有资源释放完成之后，清空资源，再次发起重连操作");
//    	    executor.execute(new Runnable() {
//    		@Override
//    		public void run() {
//    		    try {
//    			TimeUnit.SECONDS.sleep(1);
//    			try {
//    			    connect(port,host);// 发起重连操作
//    			} catch (Exception e) {
//    			    e.printStackTrace();
//    			}
//    		    } catch (InterruptedException e) {
//    			e.printStackTrace();
//    		    }
//    		}
//    	    });
    		}
        }

        /**
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception {
        	MinaClient minaClient = new MinaClient() ;
        	Injector injector = Guice.createInjector();
        	UserDao userDao = injector.getInstance(UserDao.class);
        	List<User> listUser = userDao.query(User.class, " select * from user ");
        	for(int i=0;i<listUser.size() ;i++){
        		User user = listUser.get(i);
        		minaClient.connect(9999, "192.168.1.120", user.getLogin(), "123");
        		minaClient.connect(9999, "192.168.1.120", user.getLogin(), "123");
        		minaClient.connect(9999, "192.168.1.120", user.getLogin(), "123");
        	}
        }

}